function UpdateStyles({ app, root }) {
  app.use(async (ctx, next) => {
    await next();

    if (ctx.query.type && ctx.query.type === "style") {
      ctx.type = "js";
      ctx.body = `
            function updateStyle(content) {
       const isExist = typeof CSSStyleSheet !== undefined
       if(isExist) {
        let cssStyleSheet = new CSSStyleSheet()
        cssStyleSheet.replaceSync(content)
        document.adoptedStyleSheets = [
          ...document.adoptedStyleSheets,
          cssStyleSheet
        ]
       } else {
        let style = document.createElement('style')
        style.setAttribute('type', 'text/css')
        style.innerHTML = content
        document.head.appendChild(style)
       }
      }
       export default updateStyle
      `;
    }
  });
}

export default UpdateStyles;
